package com.example.librarysystem.Service;

import com.example.librarysystem.Dao.BooksDao;
import com.example.librarysystem.Dao.BorrowDao;
import com.example.librarysystem.entity.Borrow;
import com.example.librarysystem.entity.BorrowInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

@Service("borrowService")
public class BorrowServiceImpl implements BorrowService{
    @Autowired
    private BorrowDao borrowDao;
    @Autowired
    private BooksDao booksDao;

    @Override
    public Integer selectCount(){
        return borrowDao.selectCount();
    }

    @Override
    public Integer selectCountByBookName(String bookName){
        return borrowDao.selectCountByBookName(bookName);
    }

    @Override
    public List<BorrowInfo> selectBorrowInfo(int borrowerId, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return borrowDao.selectBorrowInfo(borrowerId, startCurrentPage, pageSize);
        }
    }
    @Override
    public List<BorrowInfo> selectBorrowInfoSearch(int borrowerId, String bookName, String author, String publishingHouse, int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return borrowDao.selectBorrowInfoSearch(borrowerId,bookName,author,publishingHouse,startCurrentPage,pageSize);
        }
    }
    @Override
    public List<Borrow> selectLastBorrow(String bookName){
        return borrowDao.selectLastBorrow(bookName);
    }
    @Override
    public List<Borrow> selectByBookName(String bookName){
        return borrowDao.selectByBookName(bookName);
    }
    @Override
    @Transactional(timeout = 30)
    public int insertBorrow(Borrow borrow, int bookId){
        booksDao.updateBookBorrow(bookId, true);
        borrowDao.insertBorrow(borrow);
        return 0;
    }
    @Override
    @Transactional(timeout = 30)
    public int updateReturn(Date end, Integer id){
        borrowDao.updateEndDate(end, id);
        borrowDao.updateIsReturn(true, id);
        int bookId = borrowDao.selectBookId(id);
        booksDao.updateBookBorrow(bookId, false);
        return 0;
    }
    @Override
    public int selectIsReturn(int id){
        return borrowDao.selectIsReturn(id);
    }
    @Override
    public Borrow selectLastBorrowByBookId(Integer id){
        return borrowDao.selectLastBorrowByBookId(id);
    }
    @Override
    @Transactional(timeout = 30)
    public int updateRenew(Date end, Integer id){
        borrowDao.updateEndDate(end, id);
        return 0;
    }
    @Override
    public int selectBorrowInfoCount(int borrowerId){
        return borrowDao.selectBorrowInfoCount(borrowerId);
    }
    @Override
    public int selectBorrowInfoSearchCount(int borrowerId, String bookName, String author, String publishingHouse){
        return borrowDao.selectBorrowInfoSearchCount(borrowerId,bookName,author,publishingHouse);
    }
    @Override
    public List<Borrow> selectBorrowAll(int currentPage, int pageSize){
        if(currentPage<=0){
            return null;
        }
        int startCurrentPage=(currentPage-1)*pageSize;        //从第几个数据开始
        int count=selectCount();
        int totalPage=count/pageSize+1;                   //总页数
        if (currentPage>totalPage){
            return null;
        }else{
            return borrowDao.selectBorrowAll(startCurrentPage, pageSize);
        }
    }
    @Override
    public Borrow selectById(int id){
        return borrowDao.selectById(id);
    }
    @Override
    public List<Borrow> selectBorrowerBetween(Date start, Date end){
        return borrowDao.selectBorrowerBetween(start,end);
    }
    @Override
    public List<Borrow> selectBorrowerOver(Date end){
        return borrowDao.selectBorrowerOver(end);
    }
}
